from Linac import *
from globals import loadLinacInfo, meminfo
import time as t

#####Build linac#####
Drifts = np.ones(150+1)*1.75 

freq=704.4e6
phi_sdeg = -15.0
length = 0
V_rf = 20.7e6
cavNum = 150
E_in = 150e6

Cavities = CavitySection(freq,phi_sdeg,length,V_rf,cavNum,E_in)
#Cavities.AddRFError(0.005,0.0014,distr='Uniform')

#####Create Pulse#####
numBunches = 5e4
tb = 1/352e6
Ib = 400e-3
P1 = Pulse(numBunches,Cavities.cavNum, tb, Ib)

Esig = 0.078e6
tsig = 0.4*np.pi/180/360/704.4e6
Ibsig = 40e-3

#P1.AddEnergyError(Esig)
#P1.AddTimeError(tsig)
P1.AddCurrentError(Ibsig)

#####Create HOMs#####
homfreq = 2.2838e9
homQex = 1e8
homRoverQ = 100

H1 = HOM(Cavities.cavNum,homfreq,homQex,homRoverQ)

fsig = 0.1e6
H1.AddFrequencyError(fsig)

#####Create Linac ready to track#####
Lin = Linac(Cavities,Drifts,P1, H1)
Lin.info()
t0 = t.time()
Lin.Track()
print 'Tracked', int(P1.numBunches), 'bunches in', t.time() - t0, 'secs'
meminfo()

